package com.tma.mobilechart.utilitys;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.opengl.GLUtils;

public class TextRenderer {
	
	private FloatBuffer vertexBuffer;	// buffer holding the vertices
	
	Canvas canvas;
  	String text = "";
  	Bitmap bitmap;
  	Paint textPaint;
  	public boolean text3D = false;
  	public boolean value3D = false;
  	public boolean textTitle = false;
	private FloatBuffer textureBuffer;	// buffer holding the texture coordinates
	
	/*private float vertices3D[] = {
			-0.92f, -0.92f,  -1.0f,		// V1 - bottom left
			-0.92f, -0.82f,  -1.0f,		// V2 - top left
			 -0.82f, -0.92f,  -1.0f,		// V3 - bottom right
			 -0.82f,  -0.82f,  -1.0f		// V4 - top right
	};
	*/
	
	/*private float verticesTitle[] = {
			-1.2f, 1.5f,  0.0f,		// V1 - bottom left
			-1.2f, 1.8f,  0.0f,		// V2 - top left
			 -0.4f, 1.5f,  0.0f,		// V3 - bottom right
			 -0.4f,  1.8f,  0.0f		// V4 - top right	
	};*/
	
	private float []verticesTitle = new float[12];

	// nghien cứu cách chuyen đổi pixels của 1 tấm bitmap sang hệ truc coordinates add vào 
	private float [] vertices3D = new float[12];
	private float texture[] = {    		
			// Mapping coordinates for the vertices
			0.0f, 1.0f,		// top left		(V2)
			0.0f, 0.0f,		// bottom left	(V1)
			1.0f, 1.0f,		// top right	(V4)
			1.0f, 0.0f		// bottom right	(V3)
	};
	
	private float []vertices2D = new float[12] ;
//	public boolean setPosText=false;
	
	
	
	/** The texture pointer */
	private int[] textures = new int[1];
	public int textSize = 40;
	private float posTextLeft;
	private float posTextRight;
	
	public TextRenderer(String text, float posTextLeft, float posTextRight)
	{
		this.text = text;
		this.posTextLeft = posTextLeft;
		this.posTextRight = posTextRight;
	}
	
	public TextRenderer(String text, int lengthText, float xTextPos, float yTextPos)
	{
		this.text=text;
		// khi khoi tao title chua set true
		setPosLeft(verticesTitle, lengthText, xTextPos, yTextPos);
		
	}
	public void setPosLeft(float [] vertices ,int lengthText,float xTextPos,float yTextPos)
	{
		for(int i =0;i<12;i++)
		 {
		 if((i+1)%3==0) 
			 {
				 if(text3D)
					 vertices[i]=-1.0f; 				 
				 else 
					 vertices[i]=0.0f;
			 }
			 
		 }
		vertices[0]= vertices[3]
				=xTextPos;
		vertices[4]= vertices[10]=yTextPos;
		vertices[1]=vertices[7]=yTextPos-0.2f;		
		
		//1 char = 0.07f
		vertices[6]=vertices[9]=xTextPos+0.07f*lengthText;
	}
	
	public void setText3D(boolean text3D)
	{
			this.text3D = text3D;
			if(text3D==true)
			{
				setVertices(vertices3D);
			}
			else 
			{
				setVertices(vertices2D);

			}
			
	}
	public void setVertices(float [] vertices)
	{
		
		   for(int i =0;i<12;i++)
			  {			
			   if((i+1)%3==0 && text3D==false)
				   vertices[i]=0;
			   else if ((i+1)%3==0 && text3D==true)
				   vertices[i]=-1.0f;
			  }
		 vertices[0]=vertices[1]=vertices[3]=vertices[7]=posTextLeft;
		 vertices[4]=vertices[6]=vertices[9]=vertices[10]=posTextRight;
	}
	
	public void setValue3D(boolean value3D,float depth3D)
	{
		//0.2f la khoang cach tu he truc den phan sau bar
			this.value3D=value3D;
			vertices3D[2]+=depth3D+0.2f;
			vertices3D[5]+=depth3D+0.2f;
			vertices3D[8]+=depth3D+0.2f;
			vertices3D[11]+=depth3D+0.2f;
	}

	/**
	 * Load the texture for the square
	 * @param gl
	 * @param context
	 */
	public void draw(GL10 gl) {
		// loading texture
		
		textPaint = new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setFakeBoldText(true);
		textPaint.setSubpixelText(true);
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(textSize);
	
		textPaint.setTextAlign(Paint.Align.LEFT);
		//	bitmap = Bitmap.createBitmap((int)textPaint.measureText(text),textSize+5, Bitmap.Config.ARGB_8888);	//tao bitmap 
			bitmap = Bitmap.createBitmap((int)textPaint.measureText(text),64, Bitmap.Config.ARGB_8888);	//tao bitmap 
		 

	//	bitmap = Bitmap.createBitmap((int)textPaint.measureText(text),(int)textPaint.getTextSize(),Bitmap.Config.ARGB_8888);
		
		canvas = new Canvas(bitmap);
		bitmap.eraseColor(0);
		//	canvas.drawText(text, 0,textSize, textPaint);
		
		canvas.drawText(text,0,50, textPaint);	
		
	
			
		// a float has 4 bytes so we allocate for each coordinate 4 bytes
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(12 * 4);
		byteBuffer.order(ByteOrder.nativeOrder());	
		vertexBuffer = byteBuffer.asFloatBuffer();	
		if(text3D== true)
		{
			vertexBuffer.put(vertices3D);
		}
		else if (textTitle)
		{
			vertexBuffer.put(verticesTitle);
		}
		else 
		{
			  vertexBuffer.put(vertices2D);  
		}
		
		vertexBuffer.position(0);
		
		ByteBuffer byteBufferTexture = ByteBuffer.allocateDirect(texture.length * 4);
		byteBufferTexture.order(ByteOrder.nativeOrder());
		textureBuffer = byteBufferTexture.asFloatBuffer();
		textureBuffer.put(texture);
		textureBuffer.position(0);
		
		
		gl.glEnable(GL10.GL_TEXTURE_2D);			//Note : Enable Texture Mapping ( NEW )
		// generate one texture pointer
		gl.glGenTextures(1, textures, 0);
		// ...and bind it to our array
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
				
		
		//Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
//		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
//		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
	/*	gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
		            GL10.GL_CLAMP_TO_EDGE);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
		            GL10.GL_CLAMP_TO_EDGE);*/
		    
	
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// Clean up
		bitmap.recycle();
		// bind the previously generated texture
		// Point to our buffers
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);	
		// Set the face rotation
		gl.glFrontFace(GL10.GL_CW);	
		// Point to our vertex buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);	
		// Draw the vertices as triangle strip
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 12 / 3);

		//Disable the client state before leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glDisable(GL10.GL_TEXTURE_2D); //Note disable Texture view
	}
}
